package com.hanson.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.ApplyInfoBean;
import com.hanson.service.ApplyInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
@RequestMapping("/admin/applyInfo")
public class ApplyInfoController {
    @Autowired
    private ApplyInfoService applyInfoService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap modelMap, String pid) {
        return "admin/applyInfo/index";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", applyInfoService.detailApplyInfo(Integer.parseInt(id)));
        }
        return "admin/applyInfo/update";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modify(ApplyInfoBean applyInfoBean) throws Exception {
        applyInfoService.updateApplyInfo(applyInfoBean);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("修改申请信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(@PathVariable String id) throws Exception {
        applyInfoService.deleteApplyInfo(Integer.parseInt(id));
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setMsg("删除申请成功！");
        return resultMessage;
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<ApplyInfoBean> listData(int limit, int page, ApplyInfoBean applyInfoBean) {
        Page<ApplyInfoBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(applyInfoService.listApplyInfoBean(pageInfo, applyInfoBean));
    }
}
